#!/bin/bash

echo "function1: ---create android project---"
echo "function2: ---generate R.java file-a--"
echo "function3: ---build all java source code---"
echo "function4: ---generate classes.dex file---"
echo "function5: ---generate resource file---"
echo "function6: ---generate unsigned apk file---"
echo "function7: ---generate certifical file---"
echo "function8: ---generate signed apk file---"
echo "##################################"
echo "function cmd:"
echo "project"
echo "R.java"
echo "build"
echo "dex"
echo "resource"
echo "unsignedapk"
echo "cert"
echo "signedapk"
echo "##################################"

#-- options
CMD=""
PROJECT_NAME=""
PROJECT_PATH=`pwd`
TARGET_ID=""
MAIN_ACTIVITY_NAME=""
ANDROID_JAR=/home/bruce/winshare/Android/android-sdk-linux/platforms/android-19/android.jar
CONFIGURATION_FILE_PATH=$PROJECT_PATH/AndroidManifest.xml
BOOTCLASS_PATH=$ANDROID_JAR
PACKAGE_NAME=com.htc.bruce.test
PACKAGE_DIRECTORY=com/htc/bruce/test
echo "@package name is :$PACKAGE_NAME"
GEN_SOURCE_CODE_PATH=$PROJECT_PATH/gen/$PACKAGE_DIRECTORY/*.java
DEX_FILENAME=classes.dex
UNSIGNED_APK_NAME=android_unsigned.apk
KEYSTORE_ALIAS=bruce_keystore
KEYSTORE_PATH=./bin/
KEYSTORE_FILENAME=bruce.keystore
SIGNED_APK_NAME=android_signed.apk
TEMP_PACKAGE_NAME=androidpackage.ap_
if [ $# == 0 ]; then
    read -p "Please input the command:" CMD
    echo "@command is: $CMD"
    if [ "$CMD" == "" ] || [ $CMD == project ]; then
        read -p "Please input project name:" PROJECT_NAME
        echo "@project name is: $PROJECT_NAME"
        read -p "Please input main activity name:" MAIN_ACTIVITY_NAME
        echo "@main activity name is: $MAIN_ACTIVITY_NAME"
        read -p "Please input target id:" TARGET_ID
        echo "@target id is: $TARGET_ID"
        if [ $PROJECT_NAME == "" ] || [ $MAIN_ACTIVITY_NAME == "" ] || [ $TARGET_ID == "" ]; then
            echo "Illegal arguments!!!"
            exit 0
        fi
        android -v create project --name $PROJECT_NAME --activity $MAIN_ACTIVITY_NAME --package $PACKAGE_NAME --target $TARGET_ID --path $PROJECT_PATH
    elif [ $CMD == R.java ]; then
        mkdir gen
        aapt package -f -m -J $PROJECT_PATH/gen -S $PROJECT_PATH/res -I $ANDROID_JAR -M $CONFIGURATION_FILE_PATH
        ls -R $PROJECT_PATH/gen/$PACKAGE_DIRECTORY
    elif [ $CMD == build ]; then
        javac -encoding GBK  -bootclasspath $BOOTCLASS_PATH -d ./bin $GEN_SOURCE_CODE_PATH ./src/$PACKAGE_DIRECTORY/*.java
    elif [ $CMD == dex ]; then
        dx --dex --output $PROJECT_PATH/bin/$DEX_FILENAME $PROJECT_PATH/bin
    elif [ $CMD == resource ]; then
        mkdir assets
        aapt package -f -S $PROJECT_PATH/res -I $ANDROID_JAR -A $PROJECT_PATH/assets -M $CONFIGURATION_FILE_PATH -F $PROJECT_PATH/bin/$TEMP_PACKAGE_NAME
    elif [ $CMD == unsignedapk ]; then
        read -p "unsigned apk name is:" UNSIGNED_APK_NAME
            if [ "$UNSIGNED_APK_NAME" == "" ]; then
                UNSIGNED_APK_NAME=android_default_unsigned.apk
            fi
            echo "@unsigned apk name is: $UNSIGNED_APK_NAME"
        apkbuilder $PROJECT_PATH/bin/$UNSIGNED_APK_NAME -v -u -z ./bin/$TEMP_PACKAGE_NAME  -f ./bin/$DEX_FILENAME -rf $PROJECT_PATH/src -nf $PROJECT_PATH/libs -rj $PROJECT_PATH/libs
    elif [ $CMD == cert ]; then
        read -p "keystore alias is:" KEYSTORE_ALIAS
        if [ "$KEYSTORE_ALIAS" == "" ]; then
            KEYSTORE_ALIAS=keystore_default_alias
        fi
        echo "@keystore alias name is: $KEYSTORE_ALIAS"
        read -p "keystore file name is:" KEYSTORE_FILENAME
        if [ "$KEYSTORE_FILENAME" == "" ]; then
            KEYSTORE_FILENAME=cert.keystore
        fi
        echo "keystore filename is: $KEYSTORE_FILENAME"
        keytool -genkeypair -alias $KEYSTORE_ALIAS -keyalg RSA -validity 4000 -keystore $PROJECT_PATH/bin/$KEYSTORE_FILENAME
    elif [ $CMD == signedapk ]; then
        read -p "signed apk name is:" SIGNED_APK_NAME
        if [ "$SIGNED_APK_NAME" == "" ]; then
            SIGNED_APK_NAME=anddroid_default_signed.apk
        fi
        echo "signed apk name is: $SIGNED_APK_NAME"
        jarsigner -verbose -keystore $PROJECT_PATH/bin/$KEYSTORE_FILENAME -signedjar $PROJECT_PATH/bin/$SIGNED_APK_NAME $PROJECT_PATH/bin/$UNSIGNED_APK_NAME $KEYSTORE_ALIAS
    else
        echo "Illegal argument!"
        exit 0
    fi
else
    echo "Illegal argument!"
fi
